﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;

namespace CollisionDetectionSandbox
{
    class TitleScreen : Screen
    {
        Texture2D mTitleScreenBackground;
        Button PlayButton, HowToButton, ScoreButton, QuitButton;
        Button PlayButtonHover, HowToHover, ScoreHover, QuitHover;

        SoundEffect BGMusic;
        public static SoundEffectInstance MusicPlaying;

        ContentManager mContent;
        HowToScreen h;
        public static int count = 0;
        public static bool MusicStarted = false;

        
        

        public TitleScreen(ContentManager theContent, EventHandler theScreenEvent)
            : base(theScreenEvent)
        {
            mContent = theContent;
            
            PlayButton = new Button(theContent, "ButtonTextures/button_play2", ZeusGame.xpos+80, ZeusGame.ypos+180);
            PlayButtonHover = new Button(theContent, "ButtonTextures/buttonhover_play2", ZeusGame.xpos+80, ZeusGame.ypos+180);
            HowToButton = new Button(theContent, "ButtonTextures/button_howto2", ZeusGame.xpos+240, ZeusGame.ypos+180);
            HowToHover = new Button(theContent, "ButtonTextures/buttonhover_howto2", ZeusGame.xpos+240, ZeusGame.ypos+180);
            ScoreButton = new Button(theContent, "ButtonTextures/button_highscore2", ZeusGame.xpos+80, ZeusGame.ypos+260);
            ScoreHover = new Button(theContent, "ButtonTextures/buttonhover_highscore2", ZeusGame.xpos+80, ZeusGame.ypos+260);
            QuitButton = new Button(theContent, "ButtonTextures/button_quit2", ZeusGame.xpos+240, ZeusGame.ypos+260);
            QuitHover = new Button(theContent, "ButtonTextures/buttonhover_quit2", ZeusGame.xpos+240, ZeusGame.ypos+260);
            mTitleScreenBackground = theContent.Load<Texture2D>("Main Textures/TitleScreenFinal");
            BGMusic = theContent.Load<SoundEffect>("Sounds/startscreen_backup");
            MusicPlaying = BGMusic.CreateInstance();
            MusicPlaying.Volume = 1.0f;
            
            if (MusicPlaying.State == SoundState.Stopped)//  && MusicStarted == false)
            {
                MusicPlaying.IsLooped = true;
                MusicPlaying.Play();
                MusicStarted = true;
            }
        }


        public override void Update(GameTime theTime)
        {
            MouseState state = Mouse.GetState();
            int mx = state.X;
            int my = state.Y;
            if (state.LeftButton == ButtonState.Pressed)
            {               
                if (PlayButton.hit_image(mx, my))
                {
                    MusicPlaying.Stop();
                    MusicStarted = false;
                    count = 0;
                    ScreenEvent.Invoke(this, new EventArgs());
                }
                else if (HowToButton.hit_image(mx, my))
                {
                    MusicPlaying.Stop();
                    count = 1;
                    ScreenEvent.Invoke(this, new EventArgs());
                }
                else if (ScoreButton.hit_image(mx, my))
                {
                    MusicPlaying.Stop(true);
                    count = 2;
                    ScreenEvent.Invoke(this, new EventArgs());
                }
                else if (QuitButton.hit_image(mx, my))
                {
                    MusicPlaying.Stop(true);
                    count = 3;
                    ScreenEvent.Invoke(this, new EventArgs());
                }
            }
                base.Update(theTime);
        }

        public override void Draw(SpriteBatch theBatch)
        {
            MouseState state = Mouse.GetState();
            int mx = state.X;
            int my = state.Y;
            theBatch.Draw(mTitleScreenBackground, Vector2.Zero, Color.White);
            
            if (PlayButtonHover.hit_image(mx, my))
            {
                PlayButtonHover.Draw(theBatch);
            }
            else  PlayButton.Draw(theBatch);

            if (HowToButton.hit_image(mx, my))
            {
                HowToHover.Draw(theBatch);
            }
            else HowToButton.Draw(theBatch);

            if (ScoreButton.hit_image(mx, my))
            {
                ScoreHover.Draw(theBatch);
            }
            else ScoreButton.Draw(theBatch);

            if(QuitButton.hit_image(mx, my))
            {
                QuitHover.Draw(theBatch);
            }
            else QuitButton.Draw(theBatch);

            base.Draw(theBatch);
        }
    }
}
